package com.passioneffect.parser {
	import com.passioneffect.parser.IXMLParser;

	/**
	 * @author Passion Effect
	 */
	public class TreeXMLParser implements IXMLParser {
		
		static public function parse(xml : XML) : Array {
			var nodes : XMLList = xml.children();
			var node : XML;
			var arr : Array = [];
			for each(node in nodes) {
				arr.push(processNode(node));
			}
			return arr;
		}

		static private function processNode(xml : XML) : Object {
			var obj = {};
			var node : XML;
			var nodes : XMLList ;
			
			if(xml.children().length()>1){
				var subs = [];
				nodes= xml.children();
				
				for each(node in nodes) {
					subs.push(processNode(node));
				}
				obj.id=xml.attribute("id");
				obj.subs = subs;
			}else{
				nodes= xml.children();
				for each(node in nodes){
					obj[node.name()]=node;
				}
			}
			
			return obj;
		}
	}
}
